package com.msb.dongbao.portal.web.advice;

import com.baomidou.kaptcha.exception.KaptchaException;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;
import com.msb.dongbao.common.base.TokenException;
import com.msb.dongbao.common.base.result.ResultWrapper;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 目前只处理算术异常
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public ResultWrapper customException(){
        return ResultWrapper.builder().code(301).msg("统一异常").build();
    }

    /**
     * 自定义taken异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(TokenException.class)
    public ResultWrapper loginException(Exception e) {
        return ResultWrapper.getFAILBuilder().code(501).msg(e.getMessage()).build();

    }

    @ExceptionHandler(KaptchaException.class)
    public String kcaotchaException(KaptchaException e) {
        if (e instanceof KaptchaTimeoutException) {
            return "验证码过期";
        } else if (e instanceof KaptchaIncorrectException) {
            return "验证码不正确";
        } else if (e instanceof KaptchaNotFoundException) {
            return "验证码未找到";
        } else {
            return "验证码渲染失败";
        }
    }


}
